package net.sppan.base.controller.employee;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.sppan.base.common.JsonResult;
import net.sppan.base.common.PageBean;
import net.sppan.base.controller.BaseController;
import net.sppan.base.entity.Department;
import net.sppan.base.entity.Employee;
import net.sppan.base.entity.User;
import net.sppan.base.service.DeparmentService;
import net.sppan.base.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController{
    @Autowired
    private DeparmentService deparmentService;
	@Autowired
	private EmployeeService employeeService;
	@RequestMapping(value ="/index")
	public String index(){
		return "employee/index";
	}

	@RequestMapping(value = { "/getEmployeeList" })
	@ResponseBody
	public PageBean getEmployeeList(@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNumber")Integer pageNumber,
                                    @RequestParam(value = "searchText", required = false)String searchText,@RequestParam(value ="sortOrder",required = false)String sortOrder) {
		Page<Employee> page = PageHelper.startPage(pageNumber, pageSize, true);
        List<Employee> resultList = employeeService.getEmployeeList(searchText);
		PageBean<Employee> pageBean = new PageBean<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        pageBean.setContent(resultList);
		return pageBean;
	}
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        return "employee/form";
    }
    @RequestMapping(value= {"/edit"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Employee employee, ModelMap map){
        try {

            employeeService.saveEmployee(employee);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
    /** 
     * @Description: 获取所有科室
     * @Param:  
     * @return:  
     * @Author:
     */
    @RequestMapping(value = "/getAllDepartments",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getAllDepartments(){
        JsonResult jsonResult = JsonResult.success();
        List<Department> departmentList = deparmentService.getAllDepartments();
        jsonResult.setData(departmentList);
        return jsonResult;
    }
    /** 
     * @Description: 编辑数据回显
     * @Param:  
     * @return:  
     * @Author:
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, ModelMap map) {
        Employee employee = employeeService.getEmployee(id);
        map.put("employee", employee);
        return "employee/form";
    }
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable Integer id,ModelMap map) {
        try {
            employeeService.deleteEmployee(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
}
